﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using UIWindows.Commands;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;
using Kinlo.Entitys;
using UIWindows.Enums;
using static NPOI.HSSF.UserModel.HeaderFooter;
using System.ComponentModel;

namespace UIWindows.ViewModels
{
    public class DataItemConfigurationViewModel : XingLucifer.IBase.IViewStatus
    {
     
        public DisplayItemsStatic DisplayItems { get; set; }
        public List<string> DataTypeList { get; set; }
        private UserConfigStatic _userConfig;
        public SystemConfigStatic _configStatic { get; set; }

        public ObservableCollection<ObservableCollection<DisplayItemsModel>> DisplayItemsList { get; set; }
        public DataItemConfigurationViewModel(DisplayItemsStatic displayItems, UserConfigStatic userConfig, SystemConfigStatic systemConfigStatic)
        {
            _configStatic = systemConfigStatic;
            DisplayItems = displayItems;
            DataTypeList = new List<string>() { "无类型", "浮点型", "时间型", "扫码位置", "设备状态","结果" };
            _userConfig = userConfig;
            int count = 1;
            DisplayItemsList=new ObservableCollection<ObservableCollection<DisplayItemsModel>>();
            //AddDisplayItems(ProductionData.Meta.AllFields, DisplayItems.WeighingDisplayItems);
            DescriptionAttribute descriptionAttribute = null;

            if (_configStatic.SCM.MainDeviceType == MainDeviceTypeEnum.前段组装线)
            {
                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(CoilCoreLoadingData), typeof(DescriptionAttribute));
                AddDisplayItems(CoilCoreLoadingData.Meta.AllFields, DisplayItems.CoilCoreLoadingDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(CupSleeveChangingData), typeof(DescriptionAttribute));
                AddDisplayItems(CupSleeveChangingData.Meta.AllFields, DisplayItems.CupSleeveChangingDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(PoleEarFoldingData), typeof(DescriptionAttribute));
                AddDisplayItems(PoleEarFoldingData.Meta.AllFields, DisplayItems.PoleEarFoldingDataDisplayItems, descriptionAttribute.Description);


                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(ShellInsertionData), typeof(DescriptionAttribute));
                AddDisplayItems(ShellInsertionData.Meta.AllFields, DisplayItems.ShellInsertionDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(SpotoWeldData), typeof(DescriptionAttribute));
                AddDisplayItems(SpotoWeldData.Meta.AllFields, DisplayItems.SpotoWeldDataDisplayItems, descriptionAttribute.Description);


                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(FaceCushionData), typeof(DescriptionAttribute));
                AddDisplayItems(FaceCushionData.Meta.AllFields, DisplayItems.FaceCushionDataDisplayItems, descriptionAttribute.Description);



                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(GrooveRollingMachineData), typeof(DescriptionAttribute));
                AddDisplayItems(GrooveRollingMachineData.Meta.AllFields, DisplayItems.GrooveRollingMachineDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(ShortCircuitTesterData), typeof(DescriptionAttribute));
                AddDisplayItems(ShortCircuitTesterData.Meta.AllFields, DisplayItems.ShortCircuitTesterDataDisplayItems, descriptionAttribute.Description);


            }
            else if (_configStatic.SCM.MainDeviceType == MainDeviceTypeEnum.前段装盘机)
            {

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(TrayFillerLeftData), typeof(DescriptionAttribute));
                AddDisplayItems(TrayFillerLeftData.Meta.AllFields, DisplayItems.TrayFillerLeftDataDisplayItems, descriptionAttribute.Description);
          
                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(TrayFillerRightData), typeof(DescriptionAttribute));
                AddDisplayItems(TrayFillerRightData.Meta.AllFields, DisplayItems.TrayFillerRightDataDisplayItems, descriptionAttribute.Description);
          
            }
            else if (_configStatic.SCM.MainDeviceType == MainDeviceTypeEnum.中段组装线)
            {
                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(DismantlingDiscLeftData), typeof(DescriptionAttribute));
                AddDisplayItems(DismantlingDiscLeftData.Meta.AllFields, DisplayItems.DismantlingDiscLeftDataDisplayItems, descriptionAttribute.Description);
                
                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(DismantlingDiscRightData), typeof(DescriptionAttribute));
                AddDisplayItems(DismantlingDiscRightData.Meta.AllFields, DisplayItems.DismantlingDiscRightDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(CapWeldingData), typeof(DescriptionAttribute));
                AddDisplayItems(CapWeldingData.Meta.AllFields, DisplayItems.CapWeldingDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(SealingMachineData), typeof(DescriptionAttribute));
                AddDisplayItems(SealingMachineData.Meta.AllFields, DisplayItems.SealingMachineDataDisplayItems, descriptionAttribute.Description);

            }
            else if (_configStatic.SCM.MainDeviceType == MainDeviceTypeEnum.后段组装线)
            {
                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(Plate), typeof(DescriptionAttribute));
                AddDisplayItems(Plate.Meta.AllFields, DisplayItems.PlateDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(CleaningData), typeof(DescriptionAttribute));
                AddDisplayItems(CleaningData.Meta.AllFields, DisplayItems.CleaningDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(MantleData), typeof(DescriptionAttribute));
                AddDisplayItems(MantleData.Meta.AllFields, DisplayItems.MantleDataDisplayItems, descriptionAttribute.Description);

                descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(typeof(MarkingData), typeof(DescriptionAttribute));
                AddDisplayItems(MarkingData.Meta.AllFields, DisplayItems.MarkingDataDisplayItems, descriptionAttribute.Description);
            }

            //foreach (var item in ProductionData.Meta.AllFields)
            //{
            //    if (!DisplayItems.WeighingDisplayItems.Any(x => x.Name == item.Name))
            //    {
            //        count = DisplayItems.WeighingDisplayItems.Count + 1;
            //        DisplayItems.WeighingDisplayItems.Add(new DisplayItemsModel()
            //        {
            //            Index = count,
            //            Name = item.Name,
            //            IsDisplay = true,
            //            IsOutput = true,
            //            Description = item.Description,
            //            SqlName = item.Description,
            //            DataType = item.Description.Contains("时间")
            //            ? (item.Description.Contains("(s)") ? "无类型" : "时间型")
            //            : (item.Description.Contains("g") || item.Description.Contains("kpa") || item.Description.Contains("ms") || item.Description.Contains("MΩ")) ? "浮点型" : "无类型",
            //        });
            //    }
            //}

            DisplayItems.SaveDisplayItem();




        }

        void AddDisplayItems(XCode.Configuration.FieldItem[] allFields, ObservableCollection<DisplayItemsModel> displayItems,string MachineInfoName)
        {
            foreach (var item in allFields)
            {
                if (!displayItems.Any(x => x.Name == item.Name))
                {
                    var count = displayItems.Count + 1;
                    displayItems.Add(new DisplayItemsModel()
                    {
                        Index = count,
                        Name = item.Name,
                        IsDisplay = true,
                        IsOutput = true,
                        Description = item.Description,
                        SqlName = item.Description,
                        DataType = item.Description.Contains("时间")
                        ? (item.Description.Contains("(s)") ? "无类型" : "时间型")
                        : (item.Description.Contains("g") || item.Description.Contains("kpa") || item.Description.Contains("ms") || item.Description.Contains("MΩ")) ? "浮点型" : "无类型",
                    });
                }

            }
            displayItems[0].MachineInfoName = MachineInfoName;
            DisplayItemsList.Add(displayItems);
        }


        public Commands.DelegateCommand<DisplayItemsModel> SelectUpdate => new Commands.DelegateCommand<DisplayItemsModel>(sender =>
        {
            int count = 1;
            foreach (var item in DisplayItems.WeighingDisplayItems)
            {
                item.Index = count;
                count++;
            }
            DisplayItems.IsDataUpdate = true;
            DisplayItems.IsHomeUpdate = true;
        });
        public DelegateCommand UnLoadCMD => new DelegateCommand(() =>
        {
            try
            {
                DisplayItems.SaveDisplayItem();
                string msg = $"用户==[{_userConfig.LoginUser.Name}]：列表修改保存成功";
                msg.OperationLog(MessageLevelType.成功);
                _ = msg.MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
            }
            catch (Exception ex)
            {
                string msg = $"用户==[{_userConfig.LoginUser.Name}]：列表修改保存异常：{ex}";
                msg.OperationLog(MessageLevelType.成功);
                _ = msg.MessageDialogs(MessageLevelType.警告, true, PopUpTypes.Global);
            }
        });
        public void Load()
        {
        }

        public void UnLoad()
        {

        }
    }
}
